﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{

    public partial class ITXABPLANData : ViewModelBase
    {

        public VABPOS VABP
        {
            get
            {
                if (!ENVMAIN.ContainsKey("VABP"))
                {
                    ENVMAIN["VABP"] = new VABPOS();
                }
                return (VABPOS)ENVMAIN["VABP"];
            }
        }

        string ABPLFD = "X";
        int ABPOS = 1;
        public decimal LDAB05 = 1;
        public decimal LDAB55 = 1;
        public decimal LDAB45 = 1;
        public decimal LDAB50 = 1;
        DataSet SetDispo = new DataSet();
        Queue<DataSet> JobQueue;
        DataSet Set4ITX = new DataSet();
        DataRow dwIntexStd = null;
        bool IsRun = false;
        string PRLO = "", PRLB = "", PRLP = "";
        string time;
        int ABAID = 1;
        public DataSet SetMap;
        public DataSet SetExt = new System.Data.DataSet();
        public DataSet SetJob;
        public SETINTEXDB SetData;
        DataTable tbABAUFM = null;

        SETINTEXDB INTEXDB = new INTEX.DataBase.SETINTEXDB();
        string processed = "";

        public ITXABPLANData()
            : base()
        {
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public DataSet LoadEnv()
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
            dwData["APFMANDANT"] = app.Mandant;
            SResult = INTEXDBC.QueryAPFOLGE(dwData, 3);
            SReturn.Merge(SResult);
            return SReturn;
        }
        public DataSet CheckGSDAT()
        {

            dwData = INTEXDB.ICC_QUERYTB.NewRow();
            dwData["GSMANDANT"] = app.Mandant;
            SResult = INTEXDBC.QueryGSDAT(dwData, 5);

            return SResult;
        }
        public DataSet CheckWASTAMM()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
            dwData["WAMANDANT"] = app.Mandant;
            SResult = INTEXDBC.QueryWASTAMM(dwData, 6);
            SReturn.Merge(SResult);
            return SReturn;
        }

        public DataSet LoadData(string WTYPE, string KRS, string LFD)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataSet STemp = new System.Data.DataSet();
            DataRow dwData;
            try
            {
                switch (WTYPE)
                {
                    case "K":
                        SResult = INTEXBL.QueryVKK("00", KRS, LFD);
                        SReturn.Merge(SResult);
                        break;
                    case "V":
                        SResult = INTEXBL.QueryABK("00", KRS, LFD);
                        SReturn.Merge(SResult);
                        dwData = INTEXDB.ICC_QUERYTB.NewRow();
                        dwData["ABPAUFKRS"] = KRS;
                        dwData["ABPAUFLFD"] = LFD;
                        SResult = INTEXDBC.QueryKSLEIST(dwData, 2);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QueryGABEW(dwData, 0);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QueryGPBST(dwData, 0);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QueryGGBST(dwData, 0);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QueryVAKDBEZ(dwData, 0);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QueryVAART(dwData, 0);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QuerySDDAT(dwData, 0);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QueryGSMATK(dwData, 0);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QueryGSDAT(dwData, 0);
                        SReturn.Merge(SResult);
                        SResult = INTEXDBC.QueryGSCOLOR(dwData, 0);
                        SReturn.Merge(SResult);
                        break;
                    case "L":
                        SResult = INTEXBL.QueryVV("00", KRS, LFD);
                        SReturn.Merge(SResult);
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        /// <summary>
        /// 获取订单网络关系相关的所有单，一个简单的递归程序
        /// </summary>
        /// <param name="krs"></param>
        /// <param name="lfd"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public object GETON(string krs, string lfd, string pos)
        {
            Dictionary<string, string> list = new Dictionary<string, string>();
            string key0 = krs + "_" + lfd + "_" + pos;
            string key = "";
            list.Add(key0, "NO");
            DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
            dwData["ABAMANDANT"] = app.Mandant;
            dwData["C20"] = krs + "-" + Helper.Right(lfd, 4) + "-" + pos;
            DataSet SResult = INTEXDBC.QueryICC_ABAX(dwData, 8);
            foreach (DataRow rx in SResult.Tables[0].Rows)
            {
                key = rx["ABAAUFKRS"].ToString() + "_" + rx["ABAAUFLFD"].ToString() + "_" + rx["ABAAUFPOS"].ToString();
                if (rx["ABAAUFTYP"].ToString().Trim() == "N")
                    continue;
                if (!list.ContainsKey(key))
                {
                    list.Add(key, "NO");
                }
            }
            while (list.ContainsValue("NO"))
            {
                //遍历这个NO，列表
                Queue<string> qabp = new Queue<string>();
                foreach (KeyValuePair<string, string> kp in list)
                {
                    if (kp.Value == "YES")
                        continue;
                    qabp.Enqueue(kp.Key);
                }
                while (qabp.Count > 0)
                {
                    //这里实现了递归
                    GetOrderNetWork(ref list, qabp.Dequeue());
                }
            }
            list.Remove(key0);
            return list;
        }
        public void DeleteAV95(string krs, string lfd, string pos)
        {
            DeleteAV95(krs, lfd, pos, "");

        }
        public void DeleteAV95(string krs, string lfd, string pos, string skrs)
        {
            Dictionary<string, string> list = (Dictionary<string, string>)GETON(krs, lfd, pos);
            string key = krs + "_" + lfd + "_" + pos;

            foreach (KeyValuePair<string, string> kp in list)
            {
                key = kp.Key;
                krs = key.Substring(0, 2);
                lfd = key.Substring(3, 6);
                pos = key.Substring(10, 3);
                if (skrs.Length > 0)
                {
                    if (!krs.StartsWith(skrs))
                        continue;
                }
                Delete("00", krs, lfd, pos);
            }

        }
        public void DeleteHAV95(string krs, string lfd, string pos)
        {
            Dictionary<string, string> list = (Dictionary<string, string>)GETON(krs, lfd, pos);
            string key = krs + "_" + lfd + "_" + pos;

            foreach (KeyValuePair<string, string> kp in list)
            {
                key = kp.Key;
                krs = key.Substring(0, 2);
                lfd = key.Substring(3, 6);
                pos = key.Substring(10, 3);
                if (krs.Length > 0)
                {
                    if (krs.StartsWith("FB"))
                        continue;
                }
                Delete("00", krs, lfd, pos);
            }

        }
        private void GetOrderNetWork(ref Dictionary<string, string> list, string key)
        {
            string krs = key.Substring(0, 2);
            string lfd = key.Substring(3, 6);
            string pos = key.Substring(10, 3);
            //T6-001230-001
            list[key] = "YES";
            DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
            dwData["ABAMANDANT"] = app.Mandant;
            dwData["ABAVKAUFKRS"] = krs;
            dwData["ABAVKAUFLFD"] = lfd;
            dwData["ABAVKAUFPOS"] = pos;
            DataSet SResult = INTEXDBC.QueryABAUFM(dwData, 12);
            foreach (DataRow rx in SResult.Tables[0].Rows)
            {
                key = rx["ABAAUFKRS"].ToString() + "_" + rx["ABAAUFLFD"].ToString() + "_" + rx["ABAAUFPOS"].ToString();
                if (app.company == "JLT" && key.StartsWith("HZ")) continue;
                if (!list.ContainsKey(key))
                {
                    //递归方法是，在列表中加入这个新单号，标志一下
                    list.Add(key, "NO");
                }
            }
            //'J4-1100-007
        }
        public void Delete(string client, string krs, string lfd, string pos = "000")
        {
            try
            {
                VABP.DELTEABPOS(client, krs, lfd, pos);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


    }
}